如何在React Native中以空键和空值作为对象发送POST请求

时间:2019-07-02 07:55:21

标签: react-native post fetch postman

我正在尝试发送获取数据的请求,但API需要将值作为对象和空键(不为null)

Every post request must be sent as application/x-www-form-urlencoded And must send all the data through a single object. The key must be empty

这是我可以通过邮递员发送请求的方式

POST: http://www.car.go.th/api/MobileApp/getModelCar
Headers: Key = Content-Type , Value = application/x-www-form-urlencoded
Body: Key = , Value = {"brand":"CHEVROLET","BodyType":"2040050811314debb706d02b33eca8e7"} 

这是我的本机代码

fetch('http://www.car.go.th/api/MobileApp/getModelCar', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: JSON.stringify({
                   '': { "brand": "CHEVROLET","BodyType":"2040050811314debb706d02b33eca8e7"}
            }),
        })

我收到错误响应“值不能为空。\ r \ n参数名称:值”,因为API仅接受我的代码在':{“ brand”:“ CHEVROLET” ...}之前的空键。

1 个答案:

答案 0 :(得分:0)

这对我有用,请检查一下,在全局中添加您的api

var postValues = {
            brand: "CHEVROLET",
            BodyType: 2040050811314debb706d02b33eca8e7
        }

    fetch(`${Global.APIUrl}`, {

        method: 'POST',

        body: JSON.stringify(postValues)

    }).then(res => res.json())

        .then(

            (result) => {

                 "YOUR CONDITIONS from result"
            }

        )