Repository: conan-io/conan

Issue:
[bug] `conan_build_info --v2 publish` returns "ERROR: 400: Bad Request"
Still having issues with the new `conan_build_info --v2` command.  
Publishing fails with "ERROR: 400: Bad Request".  It seems that no trace messages are generated for this command so please let me know if you need any more info.

### Environment Details
  * Operating System+version: Windows 10
  * Compiler+version: n/a
  * Conan version: 1.20.4
  * Python version: 3.7.3
  * Artifactory version: 6.12.1
  * Python Packages:
    * requests          2.21.0
    * six               1.12.0

### Steps to reproduce
run `conan_build_info --v2 publish buildinfo.json --url https://my_server.artifactory.cm.my_company.biz/artifactory --user <user> --password <password>` as per the [how-to](https://docs.conan.io/en/latest/howtos/generic_ci_artifactory.html#generating-build-info-from-lockfiles-information).

### Logs (Executed commands with output)
  - Conan command output: 
`ERROR: 400: Bad Request`



Discussion:
Hi @GordonJess,

I have tried the how to with a local Artifactory instance and it worked just fine. Could it be an issue contacting the artifactory instance?

Did you use a token or a password in the command?
Can you verify you have a build info repository in your artifactory?

![image](https://user-images.githubusercontent.com/10808592/69248185-16061800-0bac-11ea-871b-4b634154f559.png)

Hi @danimtb, 

> Could it be an issue contacting the artifactory instance?

I can fetch and upload packages without any problem.

> Did you use a token or a password in the command?

I used a password.

> Can you verify you have a build info repository in your artifactory?

Yes I have an artifactory-build-info repo and I have published there before with the artifactory plugin for jenkins.
Hi @danimtb,

Can you confirm what is expected by the `url` arg?

e.g.
http://localhost:8081/artifactory
http://localhost:8081/artifactory/api/build
http://localhost:8081/artifactory/api/build/my-build/51
...
I used the first one, the same as in the docs:

```
$ conan_build_info --v2 publish buildinfo.json --url http://localhost:8090/artifactory --user admin --password password
```

I suppose so, but just in case: Does the user/password have the permissions set to publish to the build-info repo?
 Yep.

I also noticed you're not using http**s**.  Could this cause any issues...?
I don't think so but we will have to continue investigating... @czoido any idea?
Hi @GordonJess,
I have not been able to reproduce the issue... Is there anything that you can consider strange in the build_info file? Is the Issue still happening ? 
Were you able to publish anything ever using `conan_build_info --v2`? 
Have you tried uploading to a local repo like for example an Artifactory cpp-ce?
> Is there anything that you can consider strange in the build_info file?

Nothing strange about the file.  Although there is less info than I was expecting.  For example, the environment information is missing (and I didn't use the `--skip-env` flag).

> Is the Issue still happening ?

Yes, it's still happening.

> Where you able to publish anything ever using conan_build_info --v2?

Not at all.  Before I was publishing build info using the artifactory plugin for jenkins.

----

I've opened discussions with the dpt. in charge of the artifactory server and will let you know if I find out something.
I think '\n' newlines should be specified when [opening the buildinfo file](https://github.com/conan-io/conan/blob/43a016d76d62e673b35964ca15a63abd4f96f737/conans/build_info/build_info.py#L252) for publishing.  The default 'universal newlines' keeps carriage returns, which don't seem to be accepted by artifactory.
Hi @GordonJess,
For us is working in Artifactory Pro and CE without problems but we are going to investigate that in case it could be related. Have you tried to manually remove possible carriage returns in the build info and check if that solved the issue when uploading?
Thanks a lot for the clue, I'll tell you if I discover anything :)
Hi @czoido,

Yes, manually removing the carriage returns solved the issue for me.
Thanks!
Great!, thanks a lot for testing @GordonJess.
We'll get to work to fix it as soon as possible.
Hi @GordonJess,
I have not been able to reproduce the Artifactory issue yet, but if it is related to carriage returns maybe [this branch](https://github.com/czoido/conan/tree/test_build_info_jenkins) could fix it.
Could you please test if it solves your issue if you have some time?
Thanks a lot!
Hi @czoido ,

I tried the branch but unfortunately it didn't solve it for me
Sorry to hear that @GordonJess :(
Could you please upload anywhere a build info file that fails for you? and the same in a version that you can successfully upload?
With the branch I sent you I don't get any carriage returns in the files. Do you get any?
@czoido,

Oops, apologies, I didn't checkout the branch after cloning so I ended up installing the `develop` branch.

I can confirm that the branch did solve my issue!  (No carriage returns in the files!)
Thanks a lot @GordonJess, We'll try to fix it for 1.21

Resolving patch:
diff --git a/conans/build_info/build_info.py b/conans/build_info/build_info.py
--- a/conans/build_info/build_info.py
+++ b/conans/build_info/build_info.py
@@ -206,8 +206,7 @@ def dump_custom_types(obj):
                 return sorted(artifacts, key=lambda u: u.get("name") or u.get("id"))
             raise TypeError
 
-        with open(self._build_info_file, "w") as f:
-            f.write(json.dumps(ret, indent=4, default=dump_custom_types))
+        save(self._build_info_file, json.dumps(ret, indent=4, default=dump_custom_types))
 
 
 def create_build_info(output, build_info_file, lockfile, user, password, apikey):
@@ -301,8 +300,6 @@ def update_build_info(buildinfo, output_file):
     for it in buildinfo:
         with open(it) as json_data:
             data = json.load(json_data)
-
         build_info = merge_buildinfo(build_info, data)
 
-    with open(output_file, "w") as f:
-        f.write(json.dumps(build_info, indent=4))
+    save(output_file, json.dumps(build_info, indent=4))


Test patch: