Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Axios PUT request -React

I want to send a put request to modify the command part of my JSON file. Here is my JSON;

{
  "users": [
    {
      "hostname": "xxx.xxx.xxx.xx",
      "password": "password1",
      "command": "command1",
      "id": 1
    },
    {
      "hostname": "xxx.xxx.xxx.xx",
      "password": "password2",
      "command": "command2",
      "id": 2
    },
    {
      "hostname": "xxx.xx.xx.xxx",
      "password": "password3",
      "command": "command3",
      "id": 3
    }
  ]
}

In App.js I send put request like this;

stopPC(id){        
            axios.put('http://localhost:3002/users/'+id,{
              command: 'stop'
            })   
  }

And I have tried this;

axios({
            method: 'put',
            url: 'http://localhost:3002/users/'+ id,
            data: {
              hostname: id.hostname,
              password: id.password,
              command:  'stop'
            }    
  });

In both, I got the following output in the JSON file.

{
  "users": [
    {
      "command": "stop",
      "id": 1
    },
    {
      "hostname": "xxx.xxx.xxx.xx",
      "password": "password2",
      "command": "command2",
      "id": 2
    },
    {
      "hostname": "xxx.xxx.xxx.xx",
      "password": "password3",
      "command": "command3",
      "id": 3
    }
  ]
}

I want to change only the command information while keeping the hostname and password information the same. I'm not sure where I went wrong, I'd be glad if you could help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If id is of type Number (which I think it is), id.hostname and id.password would be undefined, so basically

{
  hostname: id.hostname,
  password: id.password,
  command: 'stop'
}

and

{
  command: 'stop'
}

are the same to Javascript and you're effectively sending the same payload. I think that the best way of solving this would be changing how the backend handles the incoming payload by just changing the properties that came in the request. If you don't have access to changing the backend, you would have to get or read the current stored value and use that in the payload. Something like

user = axios.get('http://localhost:3002/users/'+id).then(r => r.data)
axios.put('http://localhost:3002/users/'+id,{
            hostname: user.hostname,
            password: user.password,
            command: 'stop'
         }) 

Also, PUT is meant to be used used to replace a record, and PATCH to update the existing one, which sounds more like what you want to do. Maybe just try axios.patch and it may work.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda