citivin
citivin

Reputation: 686

How to set raw body with request-promise-native

I'm using request-promise-native module on node.js. The API I'm calling requires the keys in the header as well as Content-Type: Application/json. When using postman to test it, I have to use Body > raw to pass along the necessary parameters. That works just fine.

However, when I try the same with the request library, the body is not being submitted properly. Here's what I've tried:

const request = require('request-promise-native')

let options = {
    method: 'POST',
    uri: 'https://api.com',
    headers: {
        'X-Api-Key': <key>,
        'Content-Type': 'Application/json'
    }
}

options.body = {
    key1: 'value1',
    key2: 'value2'
}

// and since the above didn't work also this:

options.form = {
    key1: 'value1',
    key2: 'value2'
}


request(options)
   .then(function(res){ /* do something */}

I do get a response, but not based on the body paramters. How can I replicate the "raw" body?

Upvotes: 0

Views: 9825

Answers (1)

Aritra Chakraborty
Aritra Chakraborty

Reputation: 12552

The request-promise-native internally uses request-promise which in turn is a wrapper of request.
request has a option

json: true 

Which is used to pass json body params. You can use it.

Upvotes: 5

Related Questions