andy wilson
andy wilson

Reputation: 970

C# Web API Error 404 on POST

I am getting an error 404 when I try and so a post on my web api and i'm not sure why i'm getting it the url and everything are correct.

http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/?id=4584&forename=Andy&surname=Wilson&[email protected]&telephone=0166%20254%204876&mobile=0733333333&title=Mr

That is my Url to the web api code that I will put next

[HttpPost]
[Route("updatecasepersonal/")]
public string UpdateCasePersonal(string Caseid, string Title, string Forename, string Surname, string Telephone, string Email, string Mobile)
{
    using (SqlConnection con = new SqlConnection(conString))
    {
        con.Open();
        var query = $@"UPDATE TestDB.dbo.[crm-data] SET Title=" + Title + ", Forename=" + Forename + ", Surname=" + Surname + ", Telephone=" + Telephone + ", Email=" + Email + ", Mobile=" + Mobile + " WHERE Caseid=" + Caseid;
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.CommandType = CommandType.Text;
            var dtb = new DataTable();
            var da = new SqlDataAdapter(cmd);
            da.Fill(dtb);
            return "Done";
        }
    }
}

also I am doing it right trying to update my table like that? or have I done everything wrong as i'm not fluent in c# yet

can provide more code if needed

This is my code that calls the api

onUpdateClick(e) {
    this.setState({
        updatedForename: this.state.Case.Forename,
        updatedSurname: this.state.Case.Surname,
        updatedHomeTelephone: this.state.Case.Telephone,
        updatedMobileTelephone: this.state.Case.Mobile,
        updatedEmail: this.state.Case.Email,
        updatedTitle: this.state.titleValue,
        updatedPurpose: this.state.purposeValue,
        updatedMaritalStatus: this.state.maritalValue,
        updatedEmpStatus: this.state.empValue,
    }, function () {

        var id = this.state.Case.Caseid;
        var forename = this.state.updatedForename;
        var surname = this.state.updatedSurname;
        var email = this.state.updatedEmail;
        var homeTelephone = this.state.updatedHomeTelephone;
        var mobileTelephone = this.state.updatedMobileTelephone;
        var title = this.state.updatedTitle;

        axios.post('http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/', {
            params: {
                id: id,
                forename: forename,
                surname: surname,
                email: email,
                telephone: homeTelephone,
                mobile: mobileTelephone,
                title: title
            }
        }).then(function (res) {
        }).catch(function (err) {
        });

    });
    this.setState({
        hasSaved: true
    });

}

Upvotes: 2

Views: 757

Answers (1)

Jun Kang
Jun Kang

Reputation: 1275

If you really want to send the concatenated data in the URL, do something like this:

[HttpPut]
[Route("updatecasepersonal/{Caseid}/{Title}/{Forename}/{Surname}/{Email}/{Telephone}/{Mobile}")]
public string UpdateCasePersonal(string Caseid, string Title, string Forename, string Surname, string Telephone, string Email, string Mobile)
{
    ...
}

And your url should simply look like :

http://10.0.1.96/testwebapi/api/case/UpdateCasePersonal/4584/Mr/Andy/Wilson/[email protected]/0166%20254%204876/0733333333/

This is not good practice.

This completely exposes your data in the request. In general, concatenation is almost never the best way to do anything related to data. You SHOULD send the data as a whole to the call instead. Something like:

[HttpPut]
[Route("updatecasepersonal/{CaseId}"]
public string UpdateCasePersonal(string Caseid, RequestDto request)
{
    ...
}

Of course, RequestDto should be a class you make that requires all those fields: Title, Forename, Surname, Email, etc, and pass it in your Javascript (or wherever your sending the post from). And it should be named something apt to your request. Like since this looks like user profile data, or something along those lines, something like ContactDto.

Upvotes: 3

Related Questions