dtsps2098 2014-03-21 04:37
浏览 105
已采纳

使用请求传递JSON函数参数

I am trying to use the guerrillamail API located at http://api.guerrillamail.com/ajax.php, but I'm running into trouble with the "set_email_user" function, as it requires me to post arguments along with the function.

I have never worked with PHP, nor JSON, so I do not know the correct way to post arguments to a function like this.

I am using the python 2.7 library requests to make things easier.

    s = requests.session()

    payload = {'f':'get_email_address'} 
    headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'}
    req2 = s.post('http://api.guerrillamail.com/ajax.php', data = payload, headers = headers)
    print req2.text #prints out the current email address

    payload = {'f':'set_email_user:u\'sark\''} #here is the issue. I cannot figure out how to pass a string argument into the set_email_user function
    req = s.post('http://api.guerrillamail.com/ajax.php', data = payload, headers = headers, cookies = s.cookies)
    print req.text 

    payload = {'f':'get_email_address'}
    req2 = s.post('http://api.guerrillamail.com/ajax.php', data = payload, headers = headers, cookies = s.cookies)
    print req2.text #prints the same email address as above
  • 写回答

1条回答 默认 最新

  • dougu6815 2014-03-21 05:14
    关注

    A bit of reading the docs, and bit of reading between the lines, I think. There's a big header at the top that builds the idea of "arguments"; some sample code way near the bottom seems to say that if it's a "query" for data, then send a GET request with the arguments URL encoded in the query string, such as:

    payload = {'f':'get_email_address'}
    req2 = s.get('http://api.guerrillamail.com/ajax.php', params=payload)
    print req2.text #prints out the current email address
    

    … but if you're modifying data, send a POST with the arguments URL encoded in the request body:

    payload = {'f':'set_email_user', 'email_user': 'sark'}
    req2 = s.post('http://api.guerrillamail.com/ajax.php', data=payload)
    print req2.text #prints out the current email address
    

    This section of the requests docs is the one for the GET params:

    >>> payload = {'key1': 'value1', 'key2': 'value2'}
    >>> r = requests.get("http://httpbin.org/get", params=payload)
    

    And this section is for POST data.

    Last, don't lie about your User-Agent. According to the site, the API is open, and public; Python should be fine, and you don't need to obscure your User-Agent.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!