question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using FormData with http methods

See original GitHub issue

Hi! Is it possible to send formData with put, post, etc methods? When I have something like:

@Component({})
export class Foo {
  public myForm: FormGroup;

  constructor(private angular2TokenService: Angular2TokenService) {}

  bar() {
    let someFormData = new FormData();
      Object.keys(this.myForm.value).forEach(k => {
      someFormData.append(k, this.myForm.value[k]);
    });
    this.angular2TokenService.put('url', someFormData).subscribe(res => {
      console.log(res);
    });
  }
}

I haven’t someFormData in my request. I finded workaround for myself by using: http.put with all needed headers instead: tokenService.put

I do something like:

@Component({})
export class Foo {
  public myForm: FormGroup;

  constructor(private http: Http) {
    this.headers = new Headers({
      'accessToken': localStorage.getItem('accessToken'),
      'client':       localStorage.getItem('client'),
      'expiry':       localStorage.getItem('expiry'),
      'tokenType':   localStorage.getItem('tokenType'),
      'uid':          localStorage.getItem('uid')
    });
  }

  bar() {
    let someFormData = new FormData();
      Object.keys(this.myForm.value).forEach(k => {
      someFormData.append(k, this.myForm.value[k]);
    });
    this.http.put('url', someFormData, {headers: this.headers}).subscribe(res => {
      console.log(res);
    });
  }
}

My angular2-token version is v0.1.17.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
arjenbrandenburghcommented, Jun 26, 2018

@NiekPas, he means using version 6.0.0-rc.0 of angular-token

https://github.com/neroniaky/angular2-token/tree/6.x https://www.npmjs.com/package/angular-token

We’re slowly migrating away from the current implementation and moving to a HttpInterceptor solution since that plays a lot nicer with HttpClient.

1reaction
Senegalcommented, May 11, 2017

@artem-galas, you can use “request” method and append auth headers with {enctype: “multipart/form-data”}

example from my user-service class

export class UserService extends Angular2TokenService {
 ......
  updateMediaImage(model, id, value){
    let fd:FormData = new FormData();
    fd.append('media', value, 'image.png');
    let hdrs = this.currentAuthHeaders;
    hdrs.append('enctype',"multipart/form-data")
    let requestOptions = new RequestOptions({
      method: RequestMethod.Put,
      headers: hdrs,
      url: environment.apiEndpoint+'/media/'+model+'/'+id,
      body: fd
    });
    return this.request(requestOptions).toPromise().then(
        (response) => response.json()
    );
  }
.......
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Which HTTP method is used to send the form-data
Sending the form data using the 'GET' HTTP method: The GET method is used to request data from specified resources. It sends an...
Read more >
Sending form data - Learn web development | MDN
The HTTP protocol provides several ways to perform a request; HTML form data can be transmitted via a number of different methods, ...
Read more >
Can HTTP method GET be used with "multipart/form-data ...
No. As GET requests don't have a body, specifying it's Content-Type doesn't make any sense. Especially multipart/form-data , which states ...
Read more >
How to specify the HTTP method to use when sending form ...
Use the method attribute to specify the HTTP method to use when sending form-data in HTML.
Read more >
FormData - The Modern JavaScript Tutorial
FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found