title: pass JSON to HTTP POST Request
body: <p>I'm trying to make a HTTP POST request to the google QPX Express API [1] using <strong><em>nodejs</em></strong> and <strong><em>request</em></strong> [2].</p>
  
  <p>My code looks as follows:</p>
  
  <pre><code>    // create http request client to consume the QPX API
      var request = require("request")
  
      // JSON to be passed to the QPX Express API
      var requestData = {
          "request": {
              "slice": [
                  {
                      "origin": "ZRH",
                      "destination": "DUS",
                      "date": "2014-12-02"
                  }
              ],
              "passengers": {
                  "adultCount": 1,
                  "infantInLapCount": 0,
                  "infantInSeatCount": 0,
                  "childCount": 0,
                  "seniorCount": 0
              },
              "solutions": 2,
              "refundable": false
          }
      }
  
      // QPX REST API URL (I censored my api key)
      url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey"
  
      // fire request
      request({
          url: url,
          json: true,
          multipart: {
              chunked: false,
              data: [
                  {
                      'content-type': 'application/json',
                      body: requestData
                  }
              ]
          }
      }, function (error, response, body) {
          if (!error &amp;&amp; response.statusCode === 200) {
              console.log(body)
          }
          else {
  
              console.log("error: " + error)
              console.log("response.statusCode: " + response.statusCode)
              console.log("response.statusText: " + response.statusText)
          }
      })
  </code></pre>
  
  <p>What I'm trying to do is passing the JSON using the multipart argument [3].
  But instead of the proper JSON response I got an error (400 undefined).</p>
  
  <p>When I make a request using the same JSON and API Key using CURL instead, it works fine. So there's nothing wrong with my API key or JSON.</p>
  
  <p>What's wrong with my code?</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>working CURL example:</p>
  
  <p>i) I saved the JSON which I would pass to my request into a file called "request.json":</p>
  
  <pre><code>{
    "request": {
      "slice": [
        {
          "origin": "ZRH",
          "destination": "DUS",
          "date": "2014-12-02"
        }
      ],
      "passengers": {
        "adultCount": 1,
        "infantInLapCount": 0,
        "infantInSeatCount": 0,
        "childCount": 0,
        "seniorCount": 0
      },
      "solutions": 20,
      "refundable": false
    }
  }
  </code></pre>
  
  <p>ii) then, in the terminal I switched to the directory in which the newly created request.json file was located and run (myApiKey stands for my actual API Key obviously):</p>
  
  <pre><code>curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=myApiKey
  </code></pre>
  
  <p>[1] <a href="https://developers.google.com/qpx-express/" rel="noreferrer">https://developers.google.com/qpx-express/</a>
  [2] a http request client designed for nodejs: <a href="https://www.npmjs.org/package/request" rel="noreferrer">https://www.npmjs.org/package/request</a>
  [3] here is an example I found <a href="https://www.npmjs.org/package/request#multipart-related" rel="noreferrer">https://www.npmjs.org/package/request#multipart-related</a>
  [4] <a href="https://stackoverflow.com/questions/27179248/qpx-express-api-is-returning-400-parse-error">QPX Express API is returning 400 parse error</a></p>