Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a request body that's not a string #41

Closed
haf opened this issue Jan 2, 2015 · 14 comments
Closed

Support a request body that's not a string #41

haf opened this issue Jan 2, 2015 · 14 comments

Comments

@haf
Copy link
Owner

haf commented Jan 2, 2015

Title says it all...

@relentless
Copy link
Collaborator

Hi had, what would you want the body to be? Raw bytes? Fields? (Which do get encoded as a string, bit we could certainly add functions to make that easier.)

@haf
Copy link
Owner Author

haf commented Jan 3, 2015

Not always string, sometimes raw bytes like here https://github.com/SuaveIO/suave/blob/master/src/Suave/Web.fs#L216

@relentless
Copy link
Collaborator

Sorry, haf not 'had' - autocorrect user error!

Should be doable, I'll try and have a look this weekend.

@relentless
Copy link
Collaborator

OK, I've put this on github, there's now a withBodyBytes function.

The way I've implemented it is a (slightly) breaking change to the API - do you think you'd be able to have a look and see if it meets your needs, before I make a NuGet from it?

@haf
Copy link
Owner Author

haf commented Jan 16, 2015

Yes, sure - but perhaps you could see if the API matches; the change I'd need is to make these lines: https://github.com/SuaveIO/suave/blob/master/src/Suave.Testing/Testing.fs#L123-L190 work with Http.fs - and as a side-effect you would test almost the full implementation of Http.fs.

@relentless
Copy link
Collaborator

I'll give it a look if I get the chance - having problems getting the build system working on my work PC. One thing is the timeout, which there's currently no way to set with Http.fs, but would be easy to add.

@Muhomorik
Copy link
Contributor

Hi,
Also, would be nice with a key-value list. Body if often used to make Post requests on forms with correct Content-Type.
Mostly, it is application/x-www-form-urlencoded, but there may be other
text/plain
multipart/form-data
application/json

Something like:

    let request =  
        createRequest Post "http://somesite.com"  
        |> withBodyItem {name="firstName"; value="John"} 
        |> withBodyItem {name="lastName"; value="Doe"}
        |> withHeader (ContentType "application/x-www-form-urlencoded")

@relentless
Copy link
Collaborator

Hi @Muhomorik, yes that would be pretty easy to add. I'm not sure about the name, but I can't think of a better one and it matches 'withQueryStringItem'. I'd be tempted to default the content type to x-www-form-urlencoded, but that would be confusing if someone had set the content type earlier, and there might be situations where people don't want to set it. Perhaps best just let the user set it as per your example.

If you want to have a crack at it and send me a PR go for it, otherwise I'll add it when I can, won't be for at least a week or so though.

@Muhomorik
Copy link
Contributor

@relentless Not setting content type sends request anyway, so maybe a good idea to leave to the user.
I am afraid I am quite new to F# programming and not right there to send PR yet.

@haf
Copy link
Owner Author

haf commented Mar 24, 2015

Would be great to also have an interface for adding files to the request, beyond just having a body full of bytes. Otherwise I'd have to write the file encoding myself, as well as the boundary in the form.

@haf
Copy link
Owner Author

haf commented Mar 24, 2015

@haf
Copy link
Owner Author

haf commented Mar 24, 2015

I've browsed the code a bit, and I'm not sure it's a good idea to flush the body when you use setBody and setBodyBytes.

Would be better to have a ByteBody and a StreamBody as possible values to set to your Request object, so that I can re-use the request object and also be very specific about when I want the data to be 'used'/stream disposed.

@haf
Copy link
Owner Author

haf commented Mar 25, 2015

Something like this

  let body =
    // http://ftp.isi.edu/in-notes/rfc2046.txt
    String.concat "\r\n" [
      yield "--" + boundary
      yield sprintf "Content-Disposition: form-data; name=\"file\"; filename=\"%s\""
                    (escapeQuotes file.name)
      yield sprintf "Content-Type: %s" (file.guessMime ())
      use ms = new MemoryStream()
      stream.CopyTo ms
      yield ""
      yield Convert.ToBase64String (ms.ToArray())
      yield "--" + boundary + "--"
    ]

@haf
Copy link
Owner Author

haf commented Apr 5, 2015

Closing in favour of #65 and related child-issues.

@haf haf closed this as completed Apr 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants