Problem with writing POST method "/system/config" for Web Api

Hello! I have a some trouble with SyncThing version above 1.2.0. Program written on C#. First of all I get api key from config.xml then I try GET method to read current config.xml:

    public SyncThingConfig GetSystemConfig()
   {
        var res = Get("/system/config");

        if (res.ErrorException != null)
        {
            throw new SyncThingApiException(res.ErrorMessage);
        }

        return Deserialize<SyncThingConfig>(res.Content);
    }

Further I change some nodes and try to write new config using POST method:

    public HttpStatusCode SetConfiguration(SyncThingConfig config)
    {
        var res = Post("/system/config", config);

        if (res.ErrorException != null)
        {
            throw new SyncThingApiException(res.ErrorMessage);
        }

        if (res.StatusCode != HttpStatusCode.OK)
        {
            throw new SyncThingApiException(res.ErrorMessage);
        }
        
        return res.StatusCode;
    }

And after write command i wanna to restart app using POST and got exception like a :

“Basic connection closed: The connection that was supposed to work was disconnected by the server.” In “/system/config” POST method. In SyncThing console I see next:

[OMMFY] 17:30:59 INFO: API call returned HTTP error: 400 Bad Request

Preformatted text`[OMMFY] 17:31:05 INFO: quic://0.0.0.0:22000 detected NAT type: Symmetric NAT

BTW my config.xml change properly!

If I downgraded to 1.2.0 Version everything works fine… Maybe this is due to the fact that it cannot apply configuration automatically? What’s a correct way to change config using web api? PS: Get method with headers

 private IRestResponse Get(string path)
    {
        var request = new RestRequest($"rest{path}", Method.GET);
        request.AddHeader("X-API-Key", apikey);
        return client.Execute(request);
    }

Post:

private IRestResponse Post<TBody>(string path, TBody data)
    {
        var request = new RestRequest($"rest{path}", Method.POST);
        request.AddHeader("X-API-Key", apikey);
        request.RequestFormat = DataFormat.Json;
        request.AddJsonBody(JsonConvert.SerializeObject(data));
        return client.Execute(request);
    }

Bad request means it couldn’t deserialize the posted config. Perhaps it’s syntactically invalid or otherwise incorrect? The format had also evolved over time.

Missing content headers, json missing fields we expect?

Thank for your reply! It’s strange, but configuration changes as I need. I verifyed outgoing Json with the read Json configuration from web api. Then I checked it online with the verifier and formatter. I did not find any problems. But if I read and don’t change the configuration nodes, and I try write to config everything goes fine.

I created model from default config using Postman and GET request.

Well. GUI and other things do the config post, without 400 error, so I think you need to compare carefully the result after your modifications.

Perhaps you are doing some pre-flight checks for NTLM or CORS with HEAD type or something that result in a bad request.

Thank you for your answers, it’s was very helpful for me. Why such a banner comes out in Web UI? It may depend on the errors that I get?

The configuration has been saved but not activated. Syncthing must be restarted to apply the new configuration. After restarting SyncThing my config change properly…

The notice means some of the few config values that require restart was changed.

Where can I find out which settings require this?

The ones marked restart:"true" here, mainly.

2 Likes

Thank you very much!