首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >REST API POST可以在curl中运行,但不能在python中运行

REST API POST可以在curl中运行,但不能在python中运行
EN

Stack Overflow用户
提问于 2018-06-13 23:29:50
回答 1查看 353关注 0票数 0

该服务是基于.net的服务。以下是python代码

代码语言:javascript
复制
    URL = 'http://vp-testprocessor-service-bdp-dev.dev.xxxxxxxxxxxxxx.com/api/testprocessor/v1/testprocessors/requests'
    headers = {'Authorization': 'Bearer 00000000-0000-0000-adb2-3087bcbcdaf7',
               'Accept': 'applicaiton/json',
               'Content-Type': 'audio/x-wav;type=alaw;bit=16;rate=8000;channels=1',
               'User-Agent': 'Mozilla/5.0 AppleWebkit/537.36 Chrome/41.0.2272.101 Safari/537.36'}
    session = requests.Session()
    with open(input_file, 'rb') as fd:
      data = fd.read()
    request = requests.Request('POST', url, data = data, headers = headers)
    prepped = session.prepare_request(request)
    response = session.send(prepped, allow_redirects=False)

这是python3的输出。

代码语言:javascript
复制
    DEBUG:urllib3.connectionpool:06-13 07:59 Starting new HTTP connection (1): vp-testprocessor-service-bdp-dev.dev.xxxxxxxxxxxxxx.com
    DEBUG:urllib3.connectionpool:06-13 07:59 http://vp-testprocessor-service-bdp-dev.dev.xxxxxxxxxxxxxx.com:80 "POST /api/testprocessor/v1/testprocessors/requests HTTP/1.1" 400 0

这是curl中对应的命令

代码语言:javascript
复制
    curl -X POST -i http://vp-testprocessor-service-bdp-dev.dev.xxxxxxxxxxxxxx.com/api/testprocessor/v1/testprocessors/requests -H "Accept:application/json" -H "Content-Type:audio/x-wav;type=alaw;bit=16;rate=8000;channels=1" -H "Authorization:Bearer 00000000-0000-0000-adb2-3087bcbcdaf7" --data-binary @test.WAV

响应是预期的响应:

代码语言:javascript
复制
    HTTP/1.1 202 Accepted
    Transfer-Encoding: chunked
    Content-Type: application/json; charset=utf-8
    Location: v1/testprocessors/requests/cjid8zvy905sd015mqzhk3tex
    Vary: Accept
    Server: nginx/1.13.9
    Set-Cookie: ApplicationGatewayAffinity=aec8bf4924904624c014f771c7c7d5052d5ca7537afccc9caa42d74a1f7e0023;Path=/;Domain=vp-testprocessor-service-bdp-dev.dev.xxxxxxxxxxxxxx.com
    Date: Wed, 13 Jun 2018 15:06:11 GMT

    {"status":"created","id":"cjid8zvy905sd015mqzhk3tex",...}

这是python请求的wireshark跟踪:

代码语言:javascript
复制
    1   0.000000000 192.168.0.75    55.555.555.555  TCP 74  38418 → 80 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=2153419701 TSecr=0 WS=128
    2   0.101349932 55.555.555.555  192.168.0.75    TCP 74  80 → 38418 [SYN, ACK] Seq=0 Ack=1 Win=8192 Len=0 MSS=1440 WS=256 SACK_PERM=1 TSval=1248614227 TSecr=2153419701
    3   0.101404983 192.168.0.75    55.555.555.555  TCP 66  38418 → 80 [ACK] Seq=1 Ack=1 Win=29312 Len=0 TSval=2153419803 TSecr=1248614227
    4   0.101499143 192.168.0.75    55.555.555.555  TCP 499 38418 → 80 [PSH, ACK] Seq=1 Ack=1 Win=29312 Len=433 TSval=2153419803 TSecr=1248614227 [TCP segment of a reassembled PDU]
    5   0.101595314 192.168.0.75    55.555.555.555  HTTP    256 POST /api/testprocessor/v1/testprocessors/requests HTTP/1.1  (audio/x-wav)
    6   0.219204806 55.555.555.555  192.168.0.75    TCP 66  80 → 38418 [ACK] Seq=1 Ack=624 Win=263424 Len=0 TSval=1248614344 TSecr=2153419803
    7   0.232013694 55.555.555.555  192.168.0.75    HTTP    347 HTTP/1.1 400 Bad Request 

这是curl请求的相应跟踪

代码语言:javascript
复制
    1   0.000000000 192.168.0.75    55.555.555.555  TCP 74  38400 → 80 [SYN] Seq=0 Win=29200 Len=0 MSS=1460 SACK_PERM=1 TSval=2153267280 TSecr=0 WS=128
    2   0.102793917 55.555.555.555  192.168.0.75    TCP 74  80 → 38400 [SYN, ACK] Seq=0 Ack=1 Win=8192 Len=0 MSS=1440 WS=256 SACK_PERM=1 TSval=1248461807 TSecr=2153267280
    3   0.102863788 192.168.0.75    55.555.555.555  TCP 66  38400 → 80 [ACK] Seq=1 Ack=1 Win=29312 Len=0 TSval=2153267383 TSecr=1248461807
    4   0.102977867 192.168.0.75    55.555.555.555  HTTP    576 POST /api/testprocessor/v1/testprocessors/requests HTTP/1.1  (audio/x-wav)
    5   0.273885394 55.555.555.555  192.168.0.75    TCP 66  80 → 38400 [ACK] Seq=1 Ack=511 Win=263424 Len=0 TSval=1248461978 TSecr=2153267383
    6   0.601893345 55.555.555.555  192.168.0.75    TCP 613 80 → 38400 [PSH, ACK] Seq=1 Ack=511 Win=263424 Len=547 TSval=1248462306 TSecr=2153267383 [TCP segment of a reassembled PDU]
    7   0.601961170 192.168.0.75    55.555.555.555  TCP 66  38400 → 80 [ACK] Seq=511 Ack=548 Win=30336 Len=0 TSval=2153267882 TSecr=1248462306
    8   0.601975534 55.555.555.555  192.168.0.75    HTTP    71  HTTP/1.1 202 Accepted  (application/json)

我有几个问题,1.有没有办法从urllib3获得更多的调试信息? 2.我可以做些什么来模仿curl的行为? 3.我可以生成哪些信息来帮助调试服务器端?

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-06-14 07:08:53

我在头文件中看到了拼写错误,应该是python代码中的'Accept':'application/json‘,而不是'Accept':'applicaiton/json’

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50841042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档