問題描述
在 python 中開發時,如何在 post 請求中使用“format=json&data=”? (How do I use "format=json&data=" in post requests when developing in python?)
format=json&data={
"pickup_location": {
"pin": "110096",
"add": "address",
"phone": "1111111111",
"state": "Delhi",
"city": "Delhi",
"country": "India",
"name": "name of pickup/warehouse location registered with delhivery"
}
}
上面的數據是API文檔上要求的post的payload。
因為“format=json&data="所以不知道怎麼傳輸這個數據。
payload = {
"pickup_location": {
"pin": "110096",``
"add": "Changsha", # address of warehouse
"phone": "1111111111",
"state": "Delhi",
"city": "Delhi",
"country": "India"
}
}
payload = 'format=json&data={}'.format(payload)
r = requests.post(url_test, json=payload, headers=headers)
payload = {
'format': 'json',
'data': {
"pickup_location": {
"pin": "110096",
"add": "Changsha", # address of warehouse
"phone": "1111111111",
"state": "Delhi",
"city": "Delhi",
"country": "India"
}
}
}
payload = 'format=json&data={}'.format(payload)
r = requests.post(url_test, json=payload, headers=headers)
這是我嘗試過的兩段代碼。
最終結果是一樣的:“POST 中缺少格式鍵”。
我也上網查了一下,沒找到正確的答案。
於是來求助,3Q。
參考解法
方法 1:
I had also same problem actually payload is raw data so do this
payload = {
"pickup_location": {
"pin": "110096",``
"add": "Changsha", # address of warehouse
"phone": "1111111111",
"state": "Delhi",
"city": "Delhi",
"country": "India"
}
}
payload = f'format=json&data={json.dumps(payload,default=str,indent=4)}'
response=requests.post(url_order,data=payload,headers=headers)
方法 2:
The format and data key must be sent in body x‑www‑form‑urlencoded type
方法 3:
payload = {
"pickup_location": {
"pin": "110096",
"add": "Changsha",
"phone": "1111111111",
"state": "Delhi",
"city": "Delhi",
"country": "India"
}
}
payload = 'format=json&data={}'.format(payload).replace("'",'"') # replace is very important
r = requests.post(url_test, json=payload, headers=headers)
</code></pre>
Python strings default to double quotes on the outside and single quotes on the inside. But the api requires double quotes for the parameter key.
方法 4:
I was same problem in spring boot .
when i was start searching .
Then i was find this question and i am writing this answer for spring boot developers.</p>
Packages
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Code
@Autowired
private RestTemplate restTemplate;
private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Token yourToken");
headers.set("Content‑Type","application/json");
return headers;
}
@Override
public Object save(ExpressDeliveryVO expressDelivery) throws JsonProcessingException {
HttpHeaders headers = getHeaders();
ObjectMapper mapper = new ObjectMapper();
HttpEntity<String> entity = new HttpEntity<>("format=json&data="+mapper.writeValueAsString(expressDelivery), headers);
return restTemplate
.postForEntity("https://staging‑express.delhivery.com/api/cmu/create.json", entity, Object.class)
.getBody();
}
(by Peter、Rajat、Viraj Singh、Peter、Arun Patel)
參考文件