在 python 中开发时,如何在 post 请求中使用“format=json&data=”?

问题描述 投票:0回答:5
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

python json http post
5个回答
2
投票

我也有同样的问题实际上有效载荷是原始数据 所以这样做

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)

1
投票

格式和数据密钥必须在正文中发送 x-www-form-urlencoded 类型


1
投票

我在 spring boot 中遇到了同样的问题。
当我开始搜索时。
然后我发现了这个问题,我正在为 spring boot 开发人员写这个答案。

套餐

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;

代码

@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();

}

0
投票
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 字符串默认在外面用双引号,在里面用单引号。但是 api 需要参数键的双引号。


0
投票

//这段代码在节点获取中对我来说工作正常

const option = {
method: 'POST',
headers: {
  Authorization: "Token " + apikey,
  accept: 'application/json',
  'Content-Type': 'application/json',
},
body: `format=json&data= ${JSON.stringify(createData)}`

}

© www.soinside.com 2019 - 2024. All rights reserved.