Abror's avatar
Level 1

Http client attachment

Hi everyone. API that worked in node js is not working in http client

const file = await fs.readFile('test.csv', {encoding: 'utf8'})
const group_data = new FormData()
group_data.append('display', 'broadcast')
group_data.append('action', 'group')
group_data.append('edit', false)
group_data.append('editCallee', '')
group_data.append('MAX_FILE_SIZE', '19922944')
group_data.append('name', 'test')
group_data.append('uri', '')
group_data.append('ftpdelete', 0)
group_data.append('update_period', ''),
group_data.append('csv', file)
await axios.post(
  "{url}",
  group_data,
  {
    headers: {
    "Content-Type": "multipart/form-data"
    }
  }
).then(res => {
  // console.log('res: ', res.data);
  console.log('Succesfully added group !!!');
});

  Http::withHeaders([
        'Content-Type' => 'multipart/form-data',
    ])->attach('csv', file_get_contents(public_path('test.csv')), 'test.csv')->post(
        "{url}",
        [
            'display' => 'broadcast',
            'action' => 'group',
            'edit' => false,
            'editCallee' => '',
            'MAX_FILE_SIZE' => '19922944',
            'name' => $name,
            'uri' => '',
            'ftpdelete' => '0',
            'update_period' => ''
        ]
    );
0 likes
6 replies
vincent15000's avatar

Are you sure it's a Laravel problem ?

What error do you get ?

According to the code you show here, it seems that you mix both Javascript and PHP.

martinbean's avatar

@abror Define “not working”. We can’t see your screen. We have no idea what “not working” means.

1 like
Abror's avatar
Level 1

@martinbean When I send request to external API using node js code, file is uploaded but in laravel http client it is not working

1 like
vincent15000's avatar

@Abror How do you know that http client is not working ?

Once again do you have an error message ? If yes, post it here please.

Please or to participate in this conversation.