在 python 中開發時,如何在 post 請求中使用“format=json&data=”? (How do I use "format=json&data=" in post requests when developing in python?)


問題描述

在 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 postman

方法 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 PeterRajatViraj SinghPeterArun Patel)

參考文件

  1. How do I use "format=json&data=" in post requests when developing in python? (CC BY‑SA 2.5/3.0/4.0)

#Python #post #HTTP #JSON






相關問題

如何從控制台中導入的文件中訪問變量的內容? (How do I access the contents of a variable from a file imported in a console?)

在 python 3.5 的輸入列表中添加美元符號、逗號和大括號 (Adding dollar signs, commas and curly brackets to input list in python 3.5)

為 KeyError 打印出奇怪的錯誤消息 (Strange error message printed out for KeyError)

django 1.9 中的 from django.views.generic.simple import direct_to_template 相當於什麼 (What is the equivalent of from django.views.generic.simple import direct_to_template in django 1.9)

查詢嵌入列表中的數組 (Querying for array in embedded list)

如何在 Python 中搜索子字符串是否在二進製文件中? (How to search if a substring is into a binary file in Python?)

為什麼要避免 while 循環? (Why avoid while loops?)

使用python的json模塊解析json請求 (Parse a json request using json module of python)

為什麼使用 py2app 模塊創建 mac 文件時出現錯誤? (Why i am getting Error when creating mac file using py2app module?)

當 python 線程在網絡調用(HTTPS)中並且發生上下文切換時會發生什麼? (What happens when the python thread is in network call(HTTPS) and the context switch happens?)

如何繪製一條帶斜率和一個點的線?Python (How to plot a line with slope and one point given? Python)

Pickle 找不到我不使用的模塊? (Pickle can't find module that I am not using?)







留言討論