0

So I have my post logic:

var data = new FormData();

this.fields.forEach(field => data.append(field.question, field.answer));

console.log(data);

this.http
    .fetch('login', {
        method: 'POST',
        body: data
    })
    .then(response => response.json())
    .then(loginResult => {
        
    });

The console.log shows the object with the correct information in it!

So I'm happy with the data being populated.

The request payload is this:

------WebKitFormBoundaryM4BOxXFW0i3zM4SY
Content-Disposition: form-data; name="passcode"

mysecretpasscode
------WebKitFormBoundaryM4BOxXFW0i3zM4SY--

Which the owin backend doesn't understand:

var form = await c.Request.ReadFormAsync();

c being the OwinContext. The output of form is one entry, with all that payload text in.

Clearly it is trying to send multipart/form-data when I want application/x-www-form-urlencoded

What am I miss configuring here?

Edit - based on possible duplicate

I tried to use that solution but it didn't solve my problem.

Community
  • 1
  • 1
Callum Linington
  • 14,213
  • 12
  • 75
  • 154
  • Possible duplicate of [Post 'x-www-form-urlencoded' content with aurelia-fetch-client](http://stackoverflow.com/questions/36067757/post-x-www-form-urlencoded-content-with-aurelia-fetch-client) – Fabio May 25 '16 at 20:45

1 Answers1

0

Your question is almost the same as this one Post 'x-www-form-urlencoded' content with aurelia-fetch-client, so I'm mostly copying and pasting here...

Instead of sending a FormData object, try to send an object as query-strings, and set the content-type to type application/x-www-form-urlencoded. Like this:

//jQuery.param returns something like ?param=1&param2=2 and so on
//params = a plain javascript object that contains the parameters to be sent
this.http.fetch(url, {
  body: $.param(params),
  method: 'post',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})
.then(response => response.json())
.then(response => { 
   //your magic here
});

Not a good solution, I know, but that's the easiest way I found so far.

Community
  • 1
  • 1
Fabio
  • 11,892
  • 1
  • 25
  • 41
  • I don't see these as solutions, more hacks.... So, the second solution isn't a solution because it completely defeats the point of `POST`ing.... I will give it a go – Callum Linington May 25 '16 at 22:00
  • yeah, I agree, the word "solution" doesn't fit in these cases... but these hacks are the only "solution" I've found so far. – Fabio May 26 '16 at 05:56
  • That didn't sort my problem out unforunately – Callum Linington May 26 '16 at 12:02
  • That's strange... I had the exactly same problem and the above hack solved it... I'll try to think in another answer – Fabio May 26 '16 at 16:42
  • Looking at it more closely it may do, but i had already hacked owin around the world and back and couldn't be bothered test it thoroughly – Callum Linington May 26 '16 at 20:09