# Upload API question

**URL:** https://discuss.roboflow.com/t/upload-api-question/143
**Category:** 🤝  Community Help
**Created:** [February 14, 2022, 10:22pm UTC](https://discuss.roboflow.com/t/upload-api-question/143 "2022-02-14T22:22:57Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 14, 2022, 10:22pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/1 "2022-02-14T22:22:57Z")

</div>

Hello! I have been trying to upload a annotation via API lately, but it always return that the annotation format is unsupported, even if i upload a exported annotation from existing images.

Are there more guidelines then what is written in the guide page?

---

<div class="post-metadata">

### Author: ![brad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/brad/32/23_2.png) [@brad](https://discuss.roboflow.com/u/brad)
#### Post date: [February 14, 2022, 11:30pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/2 "2022-02-14T23:30:24Z")

</div>

Welcome to the forum! Sorry to hear you’re having issues with the annotate API.

We don’t yet have more documentation for this beyond [the annotate via api docs](https://docs.roboflow.com/adding-data/upload-api#annotate) but I’m happy to help. Could you share more info about the API request (ideally a code snippet) and the annotation format you’re uploading?

If you could send me an example image & annotation I’d be happy to test on my side; it’s possible you’re encountering a bug but without more info it’s hard to say.

Once we figure things out we’ll be sure to update the docs to help future readers avoid this problem!

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 2:32pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/3 "2022-02-15T14:32:37Z")

</div>

Yes thanks!

Below is my code snippet and file which is used to upload the image and annotations.

Image&annotation:

> Blockquote  
> def \_uploadannotation(annotation\_str,pic\_id,picname,datasetname=‘mydataset’):

```
upload_url = "".join([
    f"https://api.roboflow.com/dataset/{datasetname}/annotate/{pic_id}",
    "?api_key=mykey",
    "&name=", f"6.xml"
])

# POST to the API
r = requests.post(upload_url, data=annotation_str, headers={
    "Content-Type": "text/plaintext"
})

# Output result
return r.json()

```

def \_uploadimg(img,picname, datasetname=‘mydb’):  
image = img  
if not isinstance(img,Image.Image):  
# Load Image with PIL  
img = cv2.cvtColor(img, cv2.COLOR\_BGR2RGB)  
image = Image.fromarray(img)

```
# Convert to JPEG Buffer
buffered = io.BytesIO()
image.save(buffered, quality=90, format="JPEG")

# Base 64 Encode
img_str = base64.b64encode(buffered.getvalue())
img_str = img_str.decode("ascii")

# Construct the URL
upload_url = "".join([
    f"https://api.roboflow.com/dataset/{datasetname}/upload",
    "?api_key=mykey",
    f"&name={picname}",
    "&split=train"
])

# POST to the API
r = requests.post(upload_url, data=img_str, headers={
    "Content-Type": "application/x-www-form-urlencoded"
})
return json.loads(r.content).get('id',None)

```

def test():  
im = PIL.Image.open(“images\_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg”)  
picid = \_uploadimg(im,“test.jpg”,“mydb”)  
ano = open(“images\_png.rf.7c0985eb122fe55a1b9b030f551eb211.xml”,“r”).read()  
annotation = \_uploadannotation(ano,picid,“images\_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg”,“mydb”)  
pass

image:  
 ![images_png.rf.7c0985eb122fe55a1b9b030f551eb211](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/4d16227bd2d6064fb2b728ebbcf2e088ae9d0821.jpeg)

xml:  
  
  
test\_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg  
test\_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg  
  
roboflow.ai  
  
  
128  
128  
3  
  
0  
  
up  
Unspecified  
0  
0  
0  
  
35  
96  
19  
111

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 2:34pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/4 "2022-02-15T14:34:03Z")

</div>

Sure!

here are the code snippets:

```auto
def _uploadannotation(annotation_str,pic_id,picname,datasetname='db'):

    upload_url = "".join([
        f"https://api.roboflow.com/dataset/{datasetname}/annotate/{pic_id}",
        "?api_key=key",
        "&name=", f"6.xml"
    ])

    # POST to the API
    r = requests.post(upload_url, data=annotation_str, headers={
        "Content-Type": "text/xml"
    })

    # Output result
    return r.json()

def _uploadimg(img,picname, datasetname='mb'):
    image = img
    if not isinstance(img,Image.Image):
        # Load Image with PIL
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        image = Image.fromarray(img)

    # Convert to JPEG Buffer
    buffered = io.BytesIO()
    image.save(buffered, quality=90, format="JPEG")

    # Base 64 Encode
    img_str = base64.b64encode(buffered.getvalue())
    img_str = img_str.decode("ascii")

    # Construct the URL
    upload_url = "".join([
        f"https://api.roboflow.com/dataset/{datasetname}/upload",
        "?api_key=key",
        f"&name={picname}",
        "&split=train"
    ])

    # POST to the API
    r = requests.post(upload_url, data=img_str, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    })
    return json.loads(r.content).get('id',None)

def test():
    im = PIL.Image.open("images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg")
    picid = _uploadimg(im,"test.jpg","mb")
    ano = open("images_png.rf.7c0985eb122fe55a1b9b030f551eb211.xml","r").read()
    annotation = _uploadannotation(ano,picid,"images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg","mb")
    pass

```

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 2:35pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/6 "2022-02-15T14:35:42Z")

</div>

and here is the xml

xml:

```auto

<annotation>
	<folder></folder>
	<filename>test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg</filename>
	<path>test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg</path>
	<source>
		<database>roboflow.ai</database>
	</source>
	<size>
		<width>128</width>
		<height>128</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>up</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<occluded>0</occluded>
		<bndbox>
			<xmin>35</xmin>
			<xmax>96</xmax>
			<ymin>19</ymin>
			<ymax>111</ymax>
		</bndbox>
	</object>
</annotation>

```

![images_png.rf.7c0985eb122fe55a1b9b030f551eb211](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/4d16227bd2d6064fb2b728ebbcf2e088ae9d0821.jpeg)

---

<div class="post-metadata">

### Author: ![brad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/brad/32/23_2.png) [@brad](https://discuss.roboflow.com/u/brad)
#### Post date: [February 15, 2022, 4:14pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/7 "2022-02-15T16:14:38Z")

</div>

Could you retry with the `Content-Type` header as `text/plain` instead of `text/xml`? I believe we use the `name=` query param to infer the type vs the `Content-Type`; it might be confusing our body parser.

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 5:57pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/8 "2022-02-15T17:57:38Z")

</div>

Thanks for the reply! I changed to text/plain and text/plaintext both but none gave a good responds, thats the reason i tried xml as well.

---

<div class="post-metadata">

### Author: ![brad](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/brad/32/23_2.png) [@brad](https://discuss.roboflow.com/u/brad)
#### Post date: [February 15, 2022, 6:08pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/9 "2022-02-15T18:08:45Z")

</div>

Ok, I’ll loop in an engineer on our team to dive in a little deeper & get back in touch.

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 6:26pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/10 "2022-02-15T18:26:12Z")

</div>

Many thanks! 😃, I hope this issue can be reproduced easily, if any additional information is necessary or the result cannot be reproduced, I will write a full standalone code to ensure the reproducibility of the issue

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 9:50pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/11 "2022-02-15T21:50:52Z")

</div>

Hello!

I tested some more variation on the uploading process and it turns out, if i use the filename “6.xml” or “6.txt”. Then the upload of anotation will fail, but with something such as “test.xml” or “test.txt” it works correctly, for now.

I’m unsure if I just missed it completely or it was working a few days ago, but by modifying the code snippet above from ‘6.xml’ to ‘test.xml’ solved my issue, the ‘text/plaintext’ or ‘text/xml’ doesn’t affect the calls as for now.

Thanks for the effort on helping me!

---

<div class="post-metadata">

### Author: ![hansent](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/hansent/32/29_2.png) [@hansent](https://discuss.roboflow.com/u/hansent)
#### Post date: [February 15, 2022, 9:58pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/12 "2022-02-15T21:58:27Z")

</div>

> [@c6222848](#):
>
> I tested some more variation on the uploading process and it turns out, if i use the filename “6.xml” or “6.txt”. Then the upload of anotation will fail, but with something such as “test.xml” or “test.txt” it works correctly, for now.

What error does the API produce when this is failing for you?

---

<div class="post-metadata">

### Author: ![c6222848](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/c6222848/32/97_2.png) [@c6222848](https://discuss.roboflow.com/u/c6222848)
#### Post date: [February 15, 2022, 10:18pm UTC](https://discuss.roboflow.com/t/upload-api-question/143/13 "2022-02-15T22:18:03Z")

</div>

After some more testing, I found out it is due to the mismatch between the uploaded images name and the annotation name.

In the test script i posted earlier, I uploaded the image under ‘test.jpg’ and annotation under ‘test.txt’, which is why it doesn’t produce an error.

The error I’m receiving under mismatched name is as follows:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/86313a9f52e1f4d2300e0bea72cce08edfcd6afa.png)
