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

FormData send all form data values as Blob #55

Closed
forestmaxime opened this issue Dec 17, 2019 · 0 comments
Closed

FormData send all form data values as Blob #55

forestmaxime opened this issue Dec 17, 2019 · 0 comments
Labels
bug Something isn't working
Milestone

Comments

@forestmaxime
Copy link

The issue seems to be related to the formDataValue function in request-builder.ts (requestBuilder.handlebars). If I check the differences between ng-swagger-gen and ng-openapi-gen when sending my request, the primitive types and JSON objects are sent as non-blob in the request contrary to what I found here in the following function.

private formDataValue(value: any): any {
    if (value === null || value === undefined) {
      return null;
    }
    if (value instanceof Blob) {
      return value;
    }
    if (typeof value === 'object') {
      return new Blob([JSON.stringify(value)], { type: 'application/json' });
    }
    return new Blob([String(value)], { type: 'text/plain' });
  }

Changing it to something around those lines seems to have fixed my issue:

private formDataValue(value: any): any {
    if (value === null || value === undefined) {
      return null;
    }
    if (value instanceof Blob) {
      return value;
    }
    
    if (typeof value === 'number' || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'object') {
      return JSON.stringify(value);
    }
   try {
      var o = JSON.parse(value);
      if (o && typeof o === 'object') {
        return JSON.stringify(o);
      }
    } catch {
    }
    return new Blob([String(value)], { type: 'text/plain' });
  }

How could we improve my dummy code and fix this in a cleaner way?

@luisfpg luisfpg added the bug Something isn't working label Dec 20, 2019
@luisfpg luisfpg added this to the 0.7.0 milestone Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants