yyy6yypls
yyy6yypls

Reputation: 11

How to pass both formData and Json in POST request

I am trying to pass both formData and an apiKey in the body of a POST request, but it seems like its not working in my code below. What is the right syntax of doing that?

static login = (formData) => {
    return fetch('/api/login', {
      method: 'POST',
      body: { formData,
              JSON.stringify({
                 apiKey: 'xxxxxxxx'
              }) },
      headers: {
        'Content-Type': 'application/json'
      },
      credentials: 'same-origin'
    }).then(r => r.json())
  }

Upvotes: 0

Views: 3708

Answers (1)

Electrox Mortem
Electrox Mortem

Reputation: 319

I'm pretty sure that's just request-promise, but here

var login = (formData) => {
    return fetch('/api/login', {
        method: 'POST',
        body: { formData:formData,
            json:JSON.stringify({
                apiKey: 'xxxxxxxx'
            })
        },
        headers: {
            'Content-Type': 'application/json'
        },
        credentials: 'same-origin'
    }).then(r => r.json())
}

The body object needs to have keys assigned to the values On the server, you can access them as body.formData and body.json

Upvotes: 1

Related Questions