http-client icon indicating copy to clipboard operation
http-client copied to clipboard

Form Data support?

Open Xendar opened this issue 3 years ago • 0 comments

Hello,

I am creating a custom GitHub Action, into which I am trying to implement an API call to PUT a file using multipart/form-data and I wonder if this is supported, or if we need to use a particular library to do so.

Basically, I'm doing this:

import * as core from '@actions/core'
import * as http from '@actions/http-client'
import * as httpAuth from '@actions/http-client/auth'
import FormData from 'form-data'
import fs from 'fs'

async function run(): Promise<void> {
  try {
    const login: string = core.getInput('login')
    core.setSecret(login)
    const password: string = core.getInput('password')
    core.setSecret(password)

    const basicAuth: httpAuth.BasicCredentialHandler = new httpAuth.BasicCredentialHandler(login, password)
    const requestOptions = {
      ignoreSslError: true
    }
    const httpClient: http.HttpClient = new http.HttpClient('my-action', [basicAuth], requestOptions)

    const myForm: FormData = new FormData()
    myForm.append('file', fs.createReadStream('./my-file.jar'))
    myForm.append('some-parameter', 'some-value')
    const myHeaders = {
        Accept: 'application/json',
        'Content-Type': 'multipart/form-data'
    }

    const response: http.HttpClientResponse = await httpClient.put(https://my-api/upload_binary, myForm, myHeaders)
  } catch (error) {
    if (error instanceof Error) {
      core.setFailed(error.message)
    }
  }
}

run()

Upon execution, I am getting this error: Error: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object And if I output the object type using core.info(\myForm type: ${typeof myForm}`)then I getmyForm type: objectwhere I would expect to get the typeFormData`.

Could it be that FormData conflicts at runtime with some other type? Maybe any other library one would know that can work for this user-case?

N.B.: I'm pretty new to GitHub actions and JS/TS, so I may just completely miss something obvious

Thanks

Xendar avatar Feb 11 '22 14:02 Xendar