简体   繁体   English

带有python字典的REST API请求正文

[英]REST API request body with python dictionary

I am working with AWS REST API.我正在使用 AWS REST API。 I am trying to invoke glue job via REST API.我正在尝试通过 REST API 调用粘合作业。 For that, I need to pass arguments as JSON object.为此,我需要将参数作为 JSON 对象传递。 And withing this json object, I should pass python dictionary as JSON value.使用这个 json 对象,我应该将 python 字典作为 JSON 值传递。 This is the request body,这是请求正文,

   {
    "Arguments":{
    "jobParameter":"{
  'key1':'keyname=subvalue',
  'key2':'value2'
}"

},
    "JobName":"jobname"
    }

python dictionary = { 'key1':'keyname=subvalue', 'key2':'value2' } python 字典 = { 'key1':'keyname=subvalue', 'key2':'value2' }

When I test the API by giving this as input, it gives an error,当我通过将其作为输入来测试 API 时,它给出了一个错误,

{ "__type": "SerializationException" } { "__type": "SerializationException" }

Please can anyone help with this?请问有人可以帮忙吗?

I think it's not recognizing as a valid JSON.我认为它不能识别为有效的 JSON。 Try send something diferente as parameter of jobParameter to test.尝试发送一些不同的东西作为 jobParameter 的参数进行测试。

This problem often occur when a json item has as value another dict.当 json 项具有另一个 dict 作为值时,通常会发生此问题。 Another solution is to pass it as a json.dumps()另一种解决方案是将其作为json.dumps()传递

import json
body = {
    "Arguments":{
    "jobParameter":{
        'key1':'keyname=subvalue',
        'key2':'value2'
        }    
    },
    "JobName":"jobname"
    }
body_parsed = json.dumps(body)

then in your request, you should do:然后在您的请求中,您应该执行以下操作:

requests.post(url=url, body=body_parsed , headers=headers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM