How do I use "format=json&data=" in post requests when developing in python?
Question:
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"
}
}
The data above is the payload of the post required on the API document.
I don’t know how to transfer this data because of “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)
These are the two pieces of code I’ve tried.
The end result is the same: “format key missing in POST”.
I also looked it up on the Internet, but I couldn’t find the right answer.
So I came to ask for help, 3Q.
Answers:
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)
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.
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.
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();
}
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)
//This code working fine for me in node fetch
const option = {
method: 'POST',
headers: {
Authorization: "Token " + apikey,
accept: 'application/json',
'Content-Type': 'application/json',
},
body: `format=json&data= ${JSON.stringify(createData)}`
}
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"
}
}
The data above is the payload of the post required on the API document.
I don’t know how to transfer this data because of “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)
These are the two pieces of code I’ve tried.
The end result is the same: “format key missing in POST”.
I also looked it up on the Internet, but I couldn’t find the right answer.
So I came to ask for help, 3Q.
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)
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.
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.
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();
}
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)
//This code working fine for me in node fetch
const option = {
method: 'POST',
headers: {
Authorization: "Token " + apikey,
accept: 'application/json',
'Content-Type': 'application/json',
},
body: `format=json&data= ${JSON.stringify(createData)}`
}