python如何发布程序_如何在python中发出发布请求

Here is the curl command:

curl -H "X-API-TOKEN: " 'http://foo.com/foo/bar' --data #

let me explain what goes into data

POST /foo/bar

Input (request JSON body)

Name Type

title string

body string

So, based on this.. I figured:

curl -H "X-API-TOKEN: " 'http://foo.com/foo/bar' --data '{"title":"foobar","body": "This body has both "double" and 'single' quotes"}'

Unfortunately, I am not able to figure that out as well (like curl from cli)

Though I would like to use python to send this request.

How do i do this?

解决方案

With the standard Python httplib and urllib libraries you can do

import httplib, urllib

headers = {'X-API-TOKEN': 'your_token_here'}

payload = "'title'='value1'&'name'='value2'"

conn = httplib.HTTPConnection("heise.de")

conn.request("POST", "", payload, headers)

response = conn.getresponse()

print response

or if you want to use the nice HTTP library called "Requests".

import requests

headers = {'X-API-TOKEN': 'your_token_here'}

payload = {'title': 'value1', 'name': 'value2'}

r = requests.post("http://foo.com/foo/bar", data=payload, headers=headers)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值