如何以JSON形式发送POST请求?

时间:2022-12-01 16:21:49
data = {
        'ids': [12, 3, 4, 5, 6 , ...]
    }
    urllib2.urlopen("http://abc.com/api/posts/create",urllib.urlencode(data))

I want to send a POST request, but one of the fields should be a list of numbers. How can I do that ? (JSON?)

我想发送一个POST请求,但是其中一个字段应该是数字列表。我怎么做呢?(JSON ?)

5 个解决方案

#1


125  

If your server is expecting the POST request to be json, then you would need to add a header, and also serialize the data for your request...

如果您的服务器期望POST请求是json,那么您需要添加一个header,并为您的请求序列化数据……

Python 2.x

Python 2. x

import json
import urllib2

data = {
        'ids': [12, 3, 4, 5, 6]
}

req = urllib2.Request('http://example.com/api/posts/create')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

Python 3.x

Python 3. x

https://*.com/a/26876308/496445

https://*.com/a/26876308/496445


If you don't specify the header, it will be the default application/x-www-form-urlencoded type.

如果没有指定标题,它将是默认的应用程序/x-www-form-urlencode类型。

#2


88  

I recommend using the incredible requests module.

我建议使用不可思议请求模块。

http://docs.python-requests.org/en/v0.10.7/user/quickstart/#custom-headers

http://docs.python-requests.org/en/v0.10.7/user/quickstart/定义header

url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

response = requests.post(url, data=json.dumps(payload), headers=headers)

#3


32  

for python 3.4.2 I found the following will work:

对于python 3.4.2,我发现以下方法可以工作:

import urllib.request
import json      

body = {'ids': [12, 14, 50]}  

myurl = "http://www.testmycode.com"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
print (jsondataasbytes)
response = urllib.request.urlopen(req, jsondataasbytes)

#4


4  

You have to add header,or you will get http 400 error. The code works well on python2.6,centos5.4

您必须添加标题,否则您将得到http 400错误。代码在python2.6,centos5.4上运行得很好

code:

代码:

    import urllib2,json

    url = 'http://www.google.com/someservice'
    postdata = {'key':'value'}

    req = urllib2.Request(url)
    req.add_header('Content-Type','application/json')
    data = json.dumps(postdata)

    response = urllib2.urlopen(req,data)

#5


3  

This works perfect for Python 3.5, if the URL contains Query String / Parameter value,

这对于Python 3.5来说是完美的,如果URL包含查询字符串/参数值,

Request URL = https://bah2.com/ws/rest/v1/concept/
Parameter value = 21f6bb43-98a1-419d-8f0c-8133669e40ca

请求URL = https://bah2.com/ws/rest/v1/concept/参数值= 21f6bb43-98a1-419d-8f0c-8133669e40ca

import requests

url = 'https://bahbah2.com/ws/rest/v1/concept/21f6bb43-98a1-419d-8f0c-8133669e40ca'
data = {"name": "Value"}
r = requests.post(url, auth=('username', 'password'), verify=False, json=data)
print(r.status_code)

#1


125  

If your server is expecting the POST request to be json, then you would need to add a header, and also serialize the data for your request...

如果您的服务器期望POST请求是json,那么您需要添加一个header,并为您的请求序列化数据……

Python 2.x

Python 2. x

import json
import urllib2

data = {
        'ids': [12, 3, 4, 5, 6]
}

req = urllib2.Request('http://example.com/api/posts/create')
req.add_header('Content-Type', 'application/json')

response = urllib2.urlopen(req, json.dumps(data))

Python 3.x

Python 3. x

https://*.com/a/26876308/496445

https://*.com/a/26876308/496445


If you don't specify the header, it will be the default application/x-www-form-urlencoded type.

如果没有指定标题,它将是默认的应用程序/x-www-form-urlencode类型。

#2


88  

I recommend using the incredible requests module.

我建议使用不可思议请求模块。

http://docs.python-requests.org/en/v0.10.7/user/quickstart/#custom-headers

http://docs.python-requests.org/en/v0.10.7/user/quickstart/定义header

url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

response = requests.post(url, data=json.dumps(payload), headers=headers)

#3


32  

for python 3.4.2 I found the following will work:

对于python 3.4.2,我发现以下方法可以工作:

import urllib.request
import json      

body = {'ids': [12, 14, 50]}  

myurl = "http://www.testmycode.com"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8')   # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
print (jsondataasbytes)
response = urllib.request.urlopen(req, jsondataasbytes)

#4


4  

You have to add header,or you will get http 400 error. The code works well on python2.6,centos5.4

您必须添加标题,否则您将得到http 400错误。代码在python2.6,centos5.4上运行得很好

code:

代码:

    import urllib2,json

    url = 'http://www.google.com/someservice'
    postdata = {'key':'value'}

    req = urllib2.Request(url)
    req.add_header('Content-Type','application/json')
    data = json.dumps(postdata)

    response = urllib2.urlopen(req,data)

#5


3  

This works perfect for Python 3.5, if the URL contains Query String / Parameter value,

这对于Python 3.5来说是完美的,如果URL包含查询字符串/参数值,

Request URL = https://bah2.com/ws/rest/v1/concept/
Parameter value = 21f6bb43-98a1-419d-8f0c-8133669e40ca

请求URL = https://bah2.com/ws/rest/v1/concept/参数值= 21f6bb43-98a1-419d-8f0c-8133669e40ca

import requests

url = 'https://bahbah2.com/ws/rest/v1/concept/21f6bb43-98a1-419d-8f0c-8133669e40ca'
data = {"name": "Value"}
r = requests.post(url, auth=('username', 'password'), verify=False, json=data)
print(r.status_code)