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

Fix body on request, convert to json only when needed #14

Closed
juvs opened this issue Nov 7, 2019 · 2 comments
Closed

Fix body on request, convert to json only when needed #14

juvs opened this issue Nov 7, 2019 · 2 comments

Comments

@juvs
Copy link
Contributor

juvs commented Nov 7, 2019

There's an assumption when doing a request and pass body into..

...
    if (body != null) {
      String contentTypeHeader;
      if (body is String) {
        bodyString = body;
        contentTypeHeader = "text/plain";
      } else if (body is Map || body is List) {
        bodyString = Common.toJson(body);

        if (bodyEncoding == RequestBodyEncoding.JSON) {
          contentTypeHeader = "application/json";
        } else if (bodyEncoding == RequestBodyEncoding.FormURLEncoded) {
          contentTypeHeader = "application/x-www-form-urlencoded";
        } else {
          throw Exception('unsupported bodyEncoding "$bodyEncoding"');
        }
      }

      if (contentTypeHeader != null && !Common.hasKeyIgnoreCase(headers, "content-type")) {
        headers["content-type"] = contentTypeHeader;
      }
    }
...

When the body is Map or List, always convert to json... but in some scenarios this is wrong, for instance, in a request like this:

POST /api/login HTTP/1.1
Host: foo.com
Content-Type: application/x-www-form-urlencoded
Accept: */*

username=someusername&password=somepassword

Data needs to pass like key/value map, so only convert to json when you establish content type to "application/json", in any other case just pass raw body to the request.

@juvs
Copy link
Contributor Author

juvs commented Nov 7, 2019

Opened a pull request #13
Sorry for the rearrange of the code... Visual Code is doing far to much...

@jossef
Copy link
Owner

jossef commented Nov 8, 2019

@juvs
Will merge & publish #13 later today

Thanks for your contribution!

@juvs juvs closed this as completed Nov 9, 2019
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

2 participants