dotnetdreamer/nativescript-http-formdata

Allow modification of other parts of the request (ex. adding Headers)

t3hmrman opened this issue · 11 comments

Thanks for the work on this library! It works fine for me at present, but it would be great if there was support for modifying other parts of the reqeust -- for example adding/removing headers sent by okhttp3.

Right now I've worked around this by sending my auth credentials in my app (Authorization header) as an additional param.

[EDIT] - Also, it seems like post resolves regardless of whether the request failed or not with an error code -- it would be nice if the response could be translated and handed back, or at least reject if it's something other than 2xx.

Hi @t3hmrman, will take alook at the post method resolve for failed request. With regard to sending headers, can you paste your sample code how you are currently doing ? so i can better understand the use case.

The workaround I'm using looks like this:

          const formData = new formdata.TNSHttpFormData();

          const params = [
            {
              data: "noop",
              fileName: CurrentUserSvc.getToken(),
              contentType: "plain/text",
              parameterName: "token",
            },
            {
              data,
              fileName: "file",
              contentType,
              parameterName: "file",
            }
          ];

          return formData.post(url, params)
            .catch(err => {
              console.log("ERROR:", err);
              throw new Errors.FileUploadFailed("File upload failed");
            });
        });

So basically, I'm attaching an extra field into the multipart/form-data to carry my session token that the app uses since I can't modify the request and add an Authorize: Bearer <token> header at present. Since it's going over HTTPS I'm pretty sure I'm safe (or at least as safe as it would be in the header anyway.

I had to modify my server code to look for this param in the multipart/form-data body instead of normally in the headers.

What the actual fix for this might look like in android would be some insertions near line 29 of TNSHttpFormData.android.ts as it sits now :

// ... other code ...
post(url: string, params: Array<TNSHttpFormDataParam>, options: RequestOptions): Promise<boolean> {
// .... a little ways down
  let request = new okhttp3.Request.Builder()
      .url(url)
      .post(requestBody);

  if (options && options.headers) {
      Object.keys(request.headers).forEach(k => request.headers(k, request.headers[k]));
  }

  request = request.build();

  // ... rest of the code

Basically the idea is to take some options and translate them in a cross-platform manner to what works for the underlying libs. You could even let the user pass in a function that will act on the okhttp3.Request.Builder itself, and let them worry about making it cross platform (i.e. checking for android or ios by themselves).

hmmm. it makes sense. I like your suggestion. Will work on it to add headers feature.

Yeah, I think it might actually be easiest to just let the person give a function, as far as actually writing the code goes, since it uses the builder pattern (then you can just put links to the documentation for okhttp3 and whatever else on the readme.

If you have an idea which one you'd like to do I can try and submit some code one of these weekends!

Thanks again for this awesome library, really saved me a bunch of time, nativescript still doesn't have straightforward support for FormData! :)

@t3hmrman feel free to submit any pull request that can make it flexible. But we also need to keep in mind iOS.

I just want to drop by to say that this plugin is really helpful! Saves a lot of my time. I wonder why Nativescript still doesn't have this straightforward support for FormData. Wew.

Anyhow, it would really be great if we could reconfigure the headers used to send.

Started writing some preliminary code to work on this on my fork

I took the liberty of refactoring a few things to separate the logic a little more which will make it harder to merge in the end but if anyone wants to take a look and comment please feel free

@t3hmrman custom headers are completed in Android. Soon will push.

hey @dotnetdreamer Ahh sorry I was too late to help then :( personally I've already worked around the issue and have been using the library happily since so no pressure, thanks again for making the library!

[EDIT] if nothing from the fork was useful I'll just delete it, no need to keep it around

changes has been pushed to master

Headers option has been added to master