使用请求和python时URL中的空格 [英] Spaces in a URL when using requests and python

查看:52
本文介绍了使用请求和python时URL中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能解释一下自己.却不做我自己的屁股.

我正在尝试使用python 3.4将网址发送到sparkcore api.

我设法从Windows命令行直接使用curl:-

  curl https://api.spark.io/v1/devices/xxxxxxxxxxxxxxx/led -d access_token = yyyyyyyyyyyyyy -d params = l1,HIGH 

一切正常.在 led -d 之间有一个空格,但这不是问题.

我已经读到 reting 在Python中使用 libcurl 做到这一点很痛苦,我看到了很多有关使用请求的消息,所以尽管我会给去吧.

所以我写了一个小例程:

 导入请求r = request.get('https://api.spark.io/v1/devices/xxxxxxxxxxxxxxxxxx/led -d access_token = yyyyyyyyyyyyyyy -d params = l1,HIGH')打印(r.url)打印(r) 

我得到回报:

 <响应[400]> 

当我检查实际发出的URL时,URL中的空格将替换为%20.这似乎是我的实际问题,因为请求添加的%20会使失败的服务器感到困惑

代码":400,错误":"invalid_request","error_description":未找到访问令牌"

我已经尝试阅读如何在不使用空格的情况下进行空格编码,但是我确实可以在正确的方向上使用指针.

谢谢

利亚姆

解决方案

URL不能包含空格.您正在使用的curl命令实际上是通过一些命令行参数(使用 -d向URL https://api.spark.io/v1/devices/xxxxxxxxxxxxxxx/led 发出请求))

curl手册页(手动)关于 -d命令行参数

-d,--data

(HTTP),将POST请求中的指定数据发送到HTTP服务器,就像浏览器在用户填写HTML表单并按下Submit按钮时所做的一样.这将导致curl使用内容类型application/x-www-form-urlencoded将数据传递到服务器.与-F,-form进行比较.

-d,-data与--data-ascii相同.要发布纯二进制数据,应改用--data-binary选项.要对表单字段的值进行URL编码,可以使用--data-urlencode.

如果在同一命令行上多次使用了这些选项中的任何一个,则指定的数据段将与一个单独的&-符号合并在一起.因此,使用'-d name = daniel -d skill = lousy'将生成一个看起来像'name = daniel& skill = lousy'的帖子块.

如果以字母@开头数据,则其余的应该是从中读取数据的文件名,或者-如果要curl从stdin中读取数据.也可以指定多个文件.因此,将使用--data @foobar从名为"foobar"的文件中发布数据.当--data被告知要从这样的文件中读取时,回车符和换行符将被删除.

也就是说 -d 用于使用内容类型 application/x-www-form-urlencoded

请求文档提供了一个很好的示例,说明了如何使用请求库进行操作:

I hope I can explain myself. with out making an arse of myself.

I am trying to use python 3.4 to send a url to a sparkcore api.

I have managed to use curl direcly from the windows command line:-

curl https://api.spark.io/v1/devices/xxxxxxxxxxxxxxx/led -d access_token=yyyyyyyyyyyyyyyy -d params=l1,HIGH

All works fine. there is a space between the led and -d, but that is not a problem.

I have read that reting to do this within python using libcurl is a big pain and I saw lots of messaged about using Requests, so I though I would give it a go.

So I wrote a small routine:

import requests

r = requests.get('https://api.spark.io/v1/devices/xxxxxxxxxxxxxxxxxx/led -d access_token=yyyyyyyyyyyyyyyyy -d params=l1,HIGH')
print(r.url)
print(r)

I get as return:

<Response [400]>

When I examine the URL which actually got sent out the spaces in the URL are replaced with %20. This seems to be my actual problem, because the %20 being added by requests are confusing the server which fails

"code": 400, "error": "invalid_request", "error_description": "The access token was not found"

I have tried reading up on how to inpractice have the spaces with out having a %20 being added by the encoding, but I really could do with a pointer in the right direction.

Thanks

Liam

解决方案

URLs cannot have spaces. The curl command you are using is actually making a request to the url https://api.spark.io/v1/devices/xxxxxxxxxxxxxxx/led with some command line arguments (using -d)

The curl man (manual) page says this about the -d command line argument

-d, --data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.

-d, --data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option. To URL-encode the value of a form field you may use --data-urlencode.

If any of these options is used more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would generate a post chunk that looks like 'name=daniel&skill=lousy'.

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. Multiple files can also be specified. Posting data from a file named 'foobar' would thus be done with --data @foobar. When --data is told to read from a file like that, carriage returns and newlines will be stripped out.

So that says -d is for sending data to the URL with the POST request using the content-type application/x-www-form-urlencoded

The requests documentation has a good example of how to do that using the requests library: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

So for your curl command, I think this should work

import requests
payload = {'access_token': 'yyyyyyyyyyyyyyyy', 'params': 'l1,HIGH'}
r = requests.post("https://api.spark.io/v1/devices/xxxxxxxxxxxxxxx/led", data=payload)
print(r.text)

这篇关于使用请求和python时URL中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆